import Cart from '../business/cart';
import ProcessStatus from './process-status';
import lodash from 'lodash';
import JSONManage from '../lib/JSONManage';
import KeyGenerator from 'uuid-key-generator';
import Calc from '../lib/calc';
import {
  OrderList
}
from '../config'

export default class Order {
  constructor(storeId, tableId) {
    const keygen = new KeyGenerator(KeyGenerator.BASE36);
    this.id = keygen.generateKey().toLocaleLowerCase();
    this.storeId = storeId; //店铺id
    this.tableId = tableId; //台座id
    this.headCount = "";
    this.description = "";
    this.cart = new Cart();
    this.processStatus = new ProcessStatus();
    this.extended = {}
      // Object.defineProperty(this.__proto__, 'subtotal', {
      //     enumerable: true,
      //     get : function(){
      //       // debugger
      //         const _self = this;
      //         let price = 0;
      //         _self.cart && _self.cart.items.forEach(function(elem){
      //           price += elem.amount || 0;
      //         });
      //         return price;
      //     }
      // });

  }

  //getter&&setter
  get subtotal() {
    // debugger
    const _self = this;
    let price = 0;
    _self.cart && _self.cart.items.forEach(function(elem) {
      price = Calc.accAdd(price, (elem.amount || 0));
    });
    // Math.round(price*Math.pow(10,2))/Math.pow(10,2)
    return price;
  }

  setNewOrder(order) {
    const self = order || this;
    this.headCount = self.headCount;
    this.amount = self.amount;
    this.cart = new Cart();
    this.cart.setNewCart(self.cart);
    this.processStatus.emptyProcessStatus();
  }

  mergeOrder(order) {
    order.headCount = this.headCount;
    order.amount += this.amount;
    if (!order.cart) {
      order.cart = {};
      order.cart.qty = 0;
      order.cart.items = [];
    }
    order.cart.qty += this.cart.qty;
    order.cart.items = order.cart.items.concat(this.cart.items);
    order.subtotal = Calc.accAdd((order.subtotal || 0), (this.subtotal || 0))
  }

  emptyOrder() {
    //人数不用清空
    // this.headCount = "";
    const keygen = new KeyGenerator(KeyGenerator.BASE36);
    this.description = "";
    this.cart.emptyCart();
    this.processStatus = new ProcessStatus() //this.processStatus.emptyProcessStatus()
    this.id = keygen.generateKey().toLocaleLowerCase();
  }
  toJSON() {
    // return item !==undefined && item!==null
    return lodash.extend({}, lodash.clone(this), {
      subtotal: this.subtotal
    });
    // return JSON.parse(JSON.stringify(this);
    // debugger
    // return JSON.stringify(this);
  }

  emptyProcessStatus() {
    this.processStatus.emptyProcessStatus();
  }

  static getOrderMsg(order) {
    let str;

    if (order.refOrderId && order.refOrderId.length > 0) {
      order.orderType = "refund";
    }
    if (order.orderType == "refund") {
      str = "订单取消，已退款";
    } else {
      switch (order.processStatus.status) {
        case "will-refund":
          str = "请申请退款"
          break;
        case "notifying":
          str = "正在通知厨房"
          break;
        case "refunding":
          str = "正在退款请稍后"
          break;
        case "received":
          {
            const i = order.serialNo.indexOf('-') + 1;
            str = "号牌" + (order.tableId || order.serialNo.substr(i));
            break;
          }

        default:
          //todo logger
          str = ""
          break;
      }
    }
    return str;
  }


  static setProcessStatus(order, processStatus, now = new Date().getTime()) {
    const refundExpiration = OrderList.refundExpiration;
    order.processStatus = new ProcessStatus(processStatus);
    if (order.status == "settled") {
      return
    }
    if (order.refOrderId && order.refOrderId.length > 0) {
      order.processStatus.addDetailFail();
      order.processStatus.addDetailRefundSuccess();
      return;
    }
    if (order.processStatus.status != "received") {
      if (now - order.closeTime <= refundExpiration) {
        order.extended.refundExpiration = ~~((now - order.closeTime) / 1000) +
          1;
      } else if (now - order.closeTime > refundExpiration) {
        order.processStatus.addDetailFail();
      }
    }
  }


  //继承的问题 内存中只保留一份 其他都是引用  todo
  static setNewOrderId(order) {

    const keygen = new KeyGenerator(KeyGenerator.BASE36);
    order.id = keygen.generateKey().toLocaleLowerCase();
    order.orderNo = order.id;
  }

  static getProcessStatusOrderByOrder(order) {
    let od = JSON.parse(JSON.stringify(order));
    od.processStatus = order.processStatus;
    return od;
  }

}
